package cn.enilu.flash.service.task;


import cn.enilu.flash.bean.entity.system.User;
import cn.enilu.flash.bean.entity.task.HelpTask;
import cn.enilu.flash.bean.entity.task.MoneyInfo;
import cn.enilu.flash.bean.vo.task.MoneyInfoVo;
import cn.enilu.flash.dao.system.UserRepository;
import cn.enilu.flash.dao.task.HelpTaskRepository;
import cn.enilu.flash.dao.task.MoneyInfoRepository;

import cn.enilu.flash.security.JwtUtil;
import cn.enilu.flash.service.BaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class MoneyInfoService extends BaseService<MoneyInfo,Long,MoneyInfoRepository>  {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private MoneyInfoRepository moneyInfoRepository;
    @Autowired
    private HelpTaskRepository taskRepository;
    /**
     * 设置金额明细并变动用户金额
     * @param taskId 任务Id
     * @param type 明细类型：1任务奖励，2余额提现，3任务支付，4任务退款，5微信充值，6月vip支付，7季vip支付，8年vip支付,9 活动赠送。
     */
    public MoneyInfo setMoneyInfo(Long taskId,Long userId, Integer type, BigDecimal money){
        User user = userRepository.getOne(userId);
        MoneyInfo moneyInfo = new MoneyInfo();
        HelpTask helpTask = null;
        if(taskId!=null){
            helpTask = taskRepository.getOne(taskId);
        }
        Calendar cal = Calendar.getInstance();
        if(user.getAccumulativeIncome()==null) user.setAccumulativeIncome(BigDecimal.ZERO);
        if(type == 1){
            moneyInfo.setMoneyExplain("完成任务["+helpTask.getTitle()+"]奖励");// 明细
            user.setAccumulativeIncome(user.getAccumulativeIncome().add(money));
        }else if (type == 2){
            moneyInfo.setMoneyExplain("提现到支付宝");
        }else if(type == 3){
            moneyInfo.setMoneyExplain("发布任务["+helpTask.getTitle()+"]支付");
        }else if(type == 4){
            moneyInfo.setMoneyExplain("任务["+helpTask.getTitle()+"]结束退款");
        }else if(type ==5){
            moneyInfo.setMoneyExplain("微信充值");
        }else if(type == 6){
            moneyInfo.setMoneyExplain("月vip支付");
            user.setIsVip(1);
            if(user.getVipExpirationDate()==null || user.getVipExpirationDate().getTime() < new Date().getTime()){
                cal.add(Calendar.MONTH, 1);
                user.setVipExpirationDate(cal.getTime());
            }else {
                cal.setTime(user.getVipExpirationDate());
                cal.add(Calendar.MONTH, 1);
                user.setVipExpirationDate(cal.getTime());
            }
        }else if(type == 7){
            moneyInfo.setMoneyExplain("季vip支付");
            user.setIsVip(1);
            if(user.getVipExpirationDate()==null || user.getVipExpirationDate().getTime() < new Date().getTime()){
                cal.add(Calendar.MONTH, 3);
                user.setVipExpirationDate(cal.getTime());
            }else {
                cal.setTime(user.getVipExpirationDate());
                cal.add(Calendar.MONTH, 3);
                user.setVipExpirationDate(cal.getTime());
            }
        }else if(type == 8){
            moneyInfo.setMoneyExplain("年vip支付");
            user.setIsVip(1);
            if(user.getVipExpirationDate()==null || user.getVipExpirationDate().getTime() < new Date().getTime()){
                cal.add(Calendar.YEAR, 1);
                user.setVipExpirationDate(cal.getTime());
            }else {
                cal.setTime(user.getVipExpirationDate());
                cal.add(Calendar.YEAR, 1);
                user.setVipExpirationDate(cal.getTime());
            }
        }else {
            throw new RuntimeException("没有该资金类型异常");
        }
        BigDecimal afterMoney = null;
        if(user.getBalance() == null) user.setBalance(BigDecimal.ZERO);
        if(type == 1 || type == 4 || type == 5){ // 加钱的类型
            afterMoney = user.getBalance().add(money); // 变动后金额
        }else { // 扣钱的类型
            afterMoney = user.getBalance().subtract(money);  // 变动后金额
        }
        moneyInfo.setAfterMoney(afterMoney);
        moneyInfo.setBeforeMoney(user.getBalance());// 变动前金额
        user.setBalance(afterMoney);
        moneyInfo.setTaskId(taskId);
        moneyInfo.setCreateBy(userId); // 所属人
        moneyInfo.setCreateTime(new Date()); // 操作日期
        moneyInfo.setMoney(money); // 变动金额
        moneyInfo.setType(type); // 明细类型
        userRepository.save(user);
        return moneyInfoRepository.save(moneyInfo);
    }

    public List<MoneyInfoVo> queryBySql(String sql) {
        List<MoneyInfo> moneyInfos = moneyInfoRepository.queryBySql(sql);
        List<MoneyInfoVo> moneyInfoVos = new ArrayList<>();
        for(MoneyInfo moneyInfo:moneyInfos){
            MoneyInfoVo vo = new MoneyInfoVo();
            BeanUtils.copyProperties(moneyInfo,vo);
            moneyInfoVos.add(vo);
        }
        return moneyInfoVos;
    }
}

